package com.kele.controller;

import com.kele.domain.User;
import com.kele.service.UserService;
import com.kele.utils.ResponseData;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author 赵奥林
 * @date 2021年05月10日 13:05
 * @Description  用户操作自己为xxxAccount ,管理员操作为xxxUser
 *                跳转方法需要忽略生成api文档，使用@ApiIgnore
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户模块",tags ={"用户及管理操作接口"})
public class UserController {

    @Resource
    private UserService userService;

    //一、用户模块

    //用户登录
    @RequestMapping(value = "/loginAccount",method = RequestMethod.POST)
    @ApiOperation(value = "用户登录",notes = "根据校验用户名和密码来进行登录")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "用户账户",required = true,paramType = "query"),
            @ApiImplicitParam(name = "password",value = "用户密码",required = true,paramType = "query")
    })
    public ResponseData<User> loginAccount( String account,
                                            String password,
                                            HttpServletRequest request){
        if (userService.checkAccount(account)!=null){
            User user = userService.loginUser(account, password);
            if (user!=null){
                request.getSession().setAttribute("User",user);
                if (request.getSession().getAttribute("User")!=null){
                    User user1= (User) request.getSession().getAttribute("User");
                    System.out.println(user1.getAccount()+";"+user1.getPassword());
                }else {
                    System.out.println("session 不存在");
                }
                return ResponseData.success("登录成功",user);
            }else {
                return ResponseData.fail("密码错误");
            }
        }else{
            return ResponseData.fail("用户名不存在");
        }
    }


    //用户注册
    @RequestMapping(value = "/insertUser",method = RequestMethod.POST)
    @ApiOperation(value = "用户注册",notes = "将信息封装成用户实体类进行注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "用户账号",required = true),
            @ApiImplicitParam(name = "password",value = "用户密码",required = true),
            @ApiImplicitParam(name = "repassword",value = "重复用户密码",required = true)
    })//@ApiParam(value = "用户账号",name ="account" )
    @ResponseBody
    public ResponseData<User> insertUser( String account,
                                          String password,
                                          String repassword){
        if (password.equals(repassword)) {
            User user=new User();
            user.setAccount(account);
            user.setPassword(password);
            if (userService.checkAccount(account)!=null){
                System.out.println(userService.checkAccount(account));
                return ResponseData.fail("用户名已存在");
            }
            userService.insertUser(user);

            return ResponseData.success("注册成功",user);
        }
        return ResponseData.fail("两次输入密码不一致");

    }

    //用户退出登录
    @RequestMapping(value = "outLogin",method = RequestMethod.POST)
    @ApiOperation(value = "用户退出登录")
    public ResponseData outLogin(HttpServletRequest request){
        //注销当前session
        request.getSession().invalidate();
        return ResponseData.success("已退出登录");
    }

    //用户修改密码(使用旧密码验证)
    @RequestMapping(value = "/updateUserPassword",method = RequestMethod.POST)
    @ApiOperation(value = "用户修改密码")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "oldPassword",name = "用户旧密码",required = true),
            @ApiImplicitParam(value = "newPassword",name = "用户新密码",required = true),
            @ApiImplicitParam(value = "rePassword",name = "重复新密码",required = true),
            @ApiImplicitParam(value = "id",name = "用户id",required = true,dataType = "int")
    })
    public ResponseData updateUserPassword(int id,
                                           String oldPassword,
                                           String newPassword,
                                           String rePassword){
        if (newPassword.equals(rePassword)){
            User user = userService.queryUserById(id);
            if (oldPassword.equals(user.getPassword())){
                user.setPassword(newPassword);
                return ResponseData.success("修改成功",user);
            }else {
               return ResponseData.fail("修改失败");
            }
        }else {
            return ResponseData.fail("两次输入密码不一致");
        }
    }


    //用户修改个人信息
    @RequestMapping(value = "/updateAccount",method = RequestMethod.PUT)
    @ApiOperation(value = "用户修改个人信息",notes = "根据id查询用户信息并进行修改操作")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",dataType = "int"),
            @ApiImplicitParam(name = "user",value = "封装的用户信息",dataType = "User")
    })
    public ResponseData updateAccount(  int id,
                                        User user){
        if (userService.updateUser(user)>0){
            return ResponseData.success("修改信息成功",user);
        }else{
            return ResponseData.fail("修改失败");
        }
    }

    //用户注销账户
    @RequestMapping(value = "/deleteAccount",method = RequestMethod.DELETE)
    @ApiOperation(value = "用户注销账户",notes = "用户自己注销账户")
    @ResponseBody
    public ResponseData deleteAccount(){
        userService.deleteAccount();
        return ResponseData.success("注销成功");
    }


    //二、管理员模块

    //管理员查询所有用户信息
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询所有用户信息",notes = "查询所有用户信息，返回json数据")
    public List<User> findAll(@ApiParam(value = "model",name="视图模型") Model model){
        List<User> all= userService.findAll();
        model.addAttribute("list",all);
        return all;
    }


    //管理员添加用户
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    @ApiOperation(value="管理员添加用户",notes = "根据用户来添加用户")
    @ApiImplicitParam(name = "user",value = "用户详细信息",required = true,dataType = "User")
    public ResponseData<User> addUser(@RequestBody User user){
        User user1 = userService.addUser(user);
        if (user1!=null){
            return ResponseData.success("添加成功",user1);
        }else {
            return ResponseData.fail("添加失败");
        }
    }


    //管理员修改用户信息
    @RequestMapping(value = "/updateUsers",method = RequestMethod.POST)
    @ApiOperation(value = "管理员修改用户信息",notes = "通过id查询用户信息再进行修改")
    @ResponseBody
    public ResponseData<User> updateUsers(@ApiParam(value = "user",name = "封装用户信息的user对象") User user,
                                            @ApiParam(value="id",name = "被修改用户信息的id")int id){
        if (userService.updateUser(user)>0){
            return ResponseData.success("修改成功",user);
        }else{
            User user1 = userService.queryByAccount(user.getAccount());
            return ResponseData.fail("修改失败",user1);
        }

    }

    //管理员删除用户

    /**
     * 删除用户
     * @param id 需要删除的用户的id
     * @return 返回一个 数据
     */

    @RequestMapping(value = "/deleteUser/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "管理员删除用户",notes = "通过用户id删除用户")
    public ResponseData deleteUser(@PathVariable("id")@ApiParam(value = "id",name = "管理员删除用户时用于定向用户的id") int id){
        if (userService.deleteUser(id)>0){
            return ResponseData.success("删除成功");
        }else {
            return ResponseData.fail("删除失败");
        }
    }


}
